/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_web
 * FILE       : LeftNavContextualInfoTag.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Apr 13, 2012  MyCodeTalks			       Created
 */

package net.lwf.web.tags;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import net.lwf.entity.WebUser;
import net.lwf.jaxb.service.TabMenuServiceFactory;
import net.lwf.jaxb.stubs.tabmenu.TopicInfo;
import net.lwf.util.web.WebUtil;
import net.lwf.web.constant.IWebConstant;

public class LeftNavContextualInfoTag extends TagSupport {
	
	private static final long serialVersionUID = 1L;
	private String ignore = "false";

	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		this.setInfo(request);
		return SKIP_BODY;
	}

	public int doEndTag() {
		return EVAL_PAGE;
	}
	
	public String getIgnore() {
		return ignore;
	}

	public void setIgnore(String ignore) {
		this.ignore = ignore;
	}

	private void setInfo(HttpServletRequest request) {
		if(ignore.equalsIgnoreCase("true")) {
			
		}
		else {
			String grade = this.getGrade(request);
			if(grade != null) {
				request.setAttribute(IWebConstant.GRADE, grade);
				Object subjectObj = WebUtil.getAttribute(request, IWebConstant.SUBJECT);
				Object topicObj = WebUtil.getAttribute(request, IWebConstant.TOPIC);
				if(subjectObj != null && topicObj != null) {
					TopicInfo topicinfo = TabMenuServiceFactory.getInstance().getMenu(grade, subjectObj.toString(), topicObj.toString());
					WebUtil.setAttribute(request, IWebConstant.TOPIC_MENU_INFO, topicinfo);
				}
			}			
		}

	}
	
	private String getGrade(HttpServletRequest request) {
		WebUser user = (WebUser) WebUtil.getAttribute(request, IWebConstant.WEB_USER);
		if(user != null) {
			return user.getUser().getGrade();
		}
		return null;
	}
}
